//
// Created by ty on 20-11-12.
//


#include <iostream>
#include <ros/ros.h>
#include <image_transport/image_transport.h>
#include <opencv2/opencv.hpp>
#include <sensor_msgs/Image.h>
#include <cv_bridge/cv_bridge.h>

using namespace std;

image_transport::Publisher pub;

void imageCallback(const sensor_msgs::ImageConstPtr &msg) {
    cv_bridge::CvImageConstPtr cv_ptr;
    try {
        cv_ptr = cv_bridge::toCvShare(msg, sensor_msgs::image_encodings::MONO16);
    }
    catch (cv_bridge::Exception &e) {
        ROS_ERROR("cv_bridge exception: %s", e.what());
        return;
    }

    cv::Mat image_output;
    // 将数据转成8-bit
    cv::normalize(cv_ptr->image,image_output,0.,255.,cv::NORM_MINMAX, CV_8U);

//    cv::imshow("opencv_window", cv_ptr->image);
//    cv::waitKey(3);

    cv_bridge::CvImage cv_output(cv_ptr->header, sensor_msgs::image_encodings::MONO8, image_output);
    pub.publish(cv_output.toImageMsg());

};

/**
 *
 */
int main(int argc, char *argv[]) {

    string topicName = "/camera/ir/image";
    ros::init(argc, argv, "gray_image_transformer");
    ros::NodeHandle nh("~");
//    const ros::Subscriber &subscriber = nh.subscribe(topicName, 1000, imageCallback);
//    const ros::Publisher pub = nh.advertise<sensor_msgs::Image>("/camera/ir/image_8", 1);
    image_transport::ImageTransport it(nh);
    const image_transport::Subscriber &sub = it.subscribe(topicName, 1, imageCallback);
    pub = it.advertise("/camera/ir/image_mono8", 1);

    std::cout << "Hello ros" << std::endl;

    ros::spin();

    return 0;
}